package ModelLayer;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The test class for PersonCon
 */
public class PersonConTest
{
    private ModelLayer.PersonCon personCo1;
    
    /**
     * Default constructor for test class PersonConTest
     */
    public PersonConTest()
    {
    }

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    @Before
    public void setUp()
    {
        personCo1 = ModelLayer.PersonCon.getInstance();
    }

    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    @After
    public void tearDown()
    {
        personCo1.clearPersons();
    }

    @Test
    /*
     * Test that a person can be added to PersonCon
     */
    public void addPersonTest()
    {
        ModelLayer.Person person1 = new ModelLayer.Person("Nichlas", "Lindholm", "9400", "Nørresundby", "41824949");
        personCo1.addPerson(person1);
        assertEquals(1, personCo1.getPersonsSize());
    }

    @Test
    /*
     * Test if a person can be removed from PersonCon
     */
    public void deletePersonTest()
    {
        ModelLayer.Person person1 = new ModelLayer.Person("Nichlas", "Lindholm", "9400", "Nørresundby", "41824949");
        personCo1.addPerson(person1);
        assertEquals(1, personCo1.getPersonsSize());
        personCo1.deletePerson(person1);
        assertEquals(0, personCo1.getPersonsSize());
    }

    @Test
    /*
     * Test that findPerson returns the person matching the search string
     */
    public void findPersonTest()
    {
        ModelLayer.Person person1 = new ModelLayer.Person("Nichlas", "Lindholm", "9400", "Nørresundby", "41824949");
        ModelLayer.Person person2 = new ModelLayer.Person("Claus", "vestertoften", "9574", "Baelum", "22164410");
        personCo1.addPerson(person1);
        personCo1.addPerson(person2);
        ModelLayer.Person person3 = personCo1.findPerson("22164410");
        assertEquals(person2, person3);
    }

    @Test
    /*
     * Test that findPerson returns null if search string matches no one in PersonCon
     */
    public void findPersonNotThere()
    {
        ModelLayer.Person person1 = new ModelLayer.Person("Nichlas", "Lindholm", "9400", "Nørresundby", "41824949");
        ModelLayer.Person person2 = new ModelLayer.Person("Claus", "vestertoften", "9574", "Baelum", "22164410");
        personCo1.addPerson(person1);
        personCo1.addPerson(person2);
        assertEquals(null, personCo1.findPerson("41414199"));        
    }

    @Test
    /*
     * Test if findLoan returns the loan which has an id corresponding to the search string
     */
    public void findLoanTest()
    {
        ModelLayer.Person person1 = new ModelLayer.Person("Nichlas", "Lindholm", "9400", "Nørresundby", "41824949");
        ModelLayer.Person person2 = new ModelLayer.Person("Claus", "vestertoften", "9574", "Baelum", "22164410");
        personCo1.addPerson(person1);
        personCo1.addPerson(person2); 
        Dvd dvd1 = new Dvd("DBDB", "Harry Potter 4", "Elton John", "1999");
        Copy copy1 = new Copy("1", "2013", "2000");
        Copy copy2 = new Copy("2", "2013", "2000");
        Copy copy3 = new Copy("3", "2013", "2000");
        dvd1.addCopy(copy1);
        dvd1.addCopy(copy2);
        dvd1.addCopy(copy3);
        Loan loan1 = new Loan("1", copy1, "06112014", 5);
        Loan loan2 = new Loan("2", copy1, "06112014", 5);
        Loan loan3 = new Loan("3", copy1, "06112014", 5);        
        person1.addLoan(loan1);
        person2.addLoan(loan2);
        person2.addLoan(loan3);
        assertEquals(loan3, personCo1.findLoan("3"));
    }
    
    @Test
    /*
     * Test if findLoan returns null if the search string doesnt have a match
     */
    public void findLoanNotThereTest()
    {
        ModelLayer.Person person1 = new ModelLayer.Person("Nichlas", "Lindholm", "9400", "Nørresundby", "41824949");
        personCo1.addPerson(person1);
        Dvd dvd1 = new Dvd("DBDB", "Harry Potter 4", "Elton John", "1999");
        Copy copy1 = new Copy("1", "2013", "2000");
        dvd1.addCopy(copy1);
        Loan loan1 = new Loan("1", copy1, "06112014", 5);       
        person1.addLoan(loan1);
        assertEquals(null, personCo1.findLoan("2"));
    }
    
    @Test
    /*
     * Test if findLoan returns null if there is no loans in ArrayList loans
     */
    public void findLoanEmptyTest()
    {
        ModelLayer.Person person1 = new ModelLayer.Person("Nichlas", "Lindholm", "9400", "Nørresundby", "41824949");
        personCo1.addPerson(person1);
        assertEquals(null, personCo1.findLoan("1"));
    }
    
    @Test
    /*
     * Tests that findLoan returns null if there are no Person object in ArrayList persons. 
     */
    public void findLoanNoPersonsTest()
    {
        assertEquals(0, personCo1.getPersonsSize());
        assertEquals(null, personCo1.findLoan("1"));        
    }
}
